package com.gabry.goodkill.actor
import akka.actor.{Actor, ActorLogging, ActorPath}
import com.gabry.goodkill.protocol.{GOOD_KILL_ID_TYPE, Purchase}

/**
 * Created by gabry on 2018/9/20 15:53
  * 用来分发抢购消息，分散压力，可以水平扩展。
  * 根据用户ID，活动ID，抢购商品的类型信息计算路由对应的商品actor的路径
  *
 */
class RegionActor(goodNumber:GOOD_KILL_ID_TYPE) extends Actor with ActorLogging{
  private def selectGoodActorPath(purchase: Purchase): String = s"/user/goods/${purchase.activityId}-good-${purchase.userKillId%goodNumber}"
  override def receive: Receive = {
    case purchase: Purchase =>
      val goodActorPath = selectGoodActorPath(purchase)
      context.actorSelection(goodActorPath) ! purchase
      //log.info(s"$self receive a Purchase command: $purchase,to $goodActorPath")
  }
}
